#include <bits/stdc++.h>
using namespace std;
int a[100005], aa[100005], num[100005], c[100005], p[20], k[100005], x[4], n, m, K, T;
string d[20];
int read(){
	char c = getchar();
	while(c != '-' && !isdigit(c)) c = getchar();
	int ch = 1;
	if(c == '-'){
		ch = -1;
		c = getchar();
	}
	int h = 0;
	while(isdigit(c)){
		h = h * 10 + (int)(c - 48);
		c = getchar();
	}
	h *= ch;
	return h;
}
void work(int dep){
	x[0] = read();
	x[1] = read();
	x[2] = read();
	x[3] = read();
	long long ans = 0;
	for(int ii = 1;ii <= m;++ii){
		for(int i = 1;i <= n;++i)
			a[i] = aa[i] ^ x[i % 4];
		for(int i = 1;i <= p[K];++i)
			num[i] = i;
		if(c[ii] == p[k[ii]]){
			for(int i = 1;i <= k[ii];++i){
				int mx = p[k[ii] - i];
				for(int j = 1;j <= mx;++j){
					if(d[i][j - 1] == '0'){
						if(a[2 * j - 1] >= i){
							a[j] = a[2 * j - 1];
							num[j] = num[2 * j - 1];
						}
						else{
							a[j] = a[2 * j];
							num[j] = num[2 * j];
						}
					}
					else{
						if(a[2 * j] >= i){
							a[j] = a[2 * j];
							num[j] = num[2 * j];
						}
						else{
							a[j] = a[2 * j - 1];
							num[j] = num[2 * j - 1];
						}
					}
				}
			}
		}
		else{
			for(int i = 1;i <= k[ii];++i){
				int mx = p[k[ii] - i];
				for(int j = 1;j <= mx;++j){
					if(!d[i][j - 1]){
						if(2 * j - 1 >= c[ii]) a[2 * j - 1] = rand();
						if(2 * j >= c[ii]) a[2 * j] = rand();
						if(aa[2 * j - 1] >= i){
							a[j] = a[2 * j - 1];
							num[j] = num[2 * j - 1];
						}
						else{
							a[j] = a[2 * j];
							num[j] = a[2 * j];
						}
					}
					else{
						if(aa[2 * j] >= i){
							a[j] = a[2 * j];
							num[j] = num[2 * j];
						}
						else{
							a[j] = a[2 * j - 1];
							num[j] = a[2 * j - 1];
						}
					}
				}
			}
		}
		if(ii == 1) ans = num[1];
		else ans ^= 1LL * ii * num[ii];
	}
	cout << ans << '\n';
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	srand(time(0));
	freopen("arena.in", "r", stdin);
	freopen("arena.out", "w", stdout);
	n = read();
	m = read();
	p[0] = 1;
	for(int i = 1;;++i){
		p[i] = p[i - 1] * 2;
		if(p[i] > n){
			K = i;
			break;
		}
	}
	for(int i = 1;i <= n;++i)
		aa[i] = read();
	for(int i = 1;i <= m;++i){
		c[i] = read();
		for(int j = 1;j <= K;++j)
			if(p[j] >= c[i]){
				k[i] = j;
				break;
			}
	}
	for(int i = 1;i <= K;++i)
		cin >> d[i];
	T = read();
	for(int i = 1;i <= T;++i)
		work(i);
	return 0;
}
